package com.nuc.wxh.vaccine_appointment.vaccines.controller.admin;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nuc.wxh.vaccine_appointment.base.result.R;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.AdminHospital;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.HospitalVaccine;
import com.nuc.wxh.vaccine_appointment.vaccines.entity.vo.HospitalVaccineVo;
import com.nuc.wxh.vaccine_appointment.vaccines.service.AdminHospitalService;
import com.nuc.wxh.vaccine_appointment.vaccines.service.HospitalVaccineService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 医院疫苗设置 前端控制器
 * </p>
 *
 * @author 王鑫浩
 * @since 2021-03-10
 */
@CrossOrigin
@Api(tags = "医院疫苗管理")
@RestController
@RequestMapping("/admin/hospital-vaccine")
public class HospitalVaccineController {
    @Autowired
    private HospitalVaccineService hospitalVaccineService;
    @Autowired
    private AdminHospitalService hospitalService;

    @ApiOperation(value = "根据ID删除医院上架疫苗", notes = "逻辑删除")
    @DeleteMapping("remove/{id}")
    public R removeById(@ApiParam(value = "医院上架疫苗id", required = true) @PathVariable String id) {
        boolean result = hospitalVaccineService.removeById(id);
        if (result) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation(value = "根据ID批量删除医院上架疫苗")
    @DeleteMapping("batch-remove")
    public R removeRows(@ApiParam(value = "医院上架疫苗ID", required = true) @RequestBody List<String> idList) {
        boolean result = hospitalVaccineService.removeByIds(idList);
        if (result) {
            return R.ok().message("删除成功");
        } else {
            return R.error().message("数据不存在");
        }
    }

    @ApiOperation("分页查询医院上架疫苗列表")
    @GetMapping("list/{page}/{limit}")
    public R listPage(@ApiParam(value = "当前页码", required = true) @PathVariable Long page,
                      @ApiParam(value = "每页记录数", required = true) @PathVariable Long limit,
                      @ApiParam(value = "查询对象条件") HospitalVaccineVo hospitalvaccineVo) {
        Page<HospitalVaccine> pageParam = new Page<>(page, limit);
        Page<HospitalVaccine> HospitalVaccinePage = hospitalVaccineService.selectPage(pageParam, hospitalvaccineVo);
        List<HospitalVaccine> records = HospitalVaccinePage.getRecords();
        long total = HospitalVaccinePage.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    @ApiOperation("新增医院上架疫苗")
    @PostMapping("save")
    public R save(@ApiParam(value = "医院上架疫苗对象", required = true) @RequestBody HospitalVaccine hospitalvaccine) {
        AdminHospital adminHospital = hospitalService.getById(hospitalvaccine.getHospitalId());
        hospitalvaccine.setHospitalName(adminHospital.getHospitalName());
        boolean result = hospitalVaccineService.save(hospitalvaccine);
        if (result) {
            return R.ok().message("保存成功");
        } else {
            return R.error().message("保存失败");
        }
    }

    @ApiOperation("医院上架疫苗修改")
    @PutMapping("update")
    public R updateById(@ApiParam(value = "医院上架疫苗对象", required = true) @RequestBody HospitalVaccine hospitalvaccine) {
        HospitalVaccine hospitalvaccine2 = hospitalVaccineService.getById(hospitalvaccine.getId());
        hospitalvaccine2.setVaccineNumber(hospitalvaccine.getVaccineNumber());
        boolean result = hospitalVaccineService.updateById(hospitalvaccine2);
        if (result) {
            return R.ok().message("修改成功");
        } else {
            return R.error().message("修改失败");
        }
    }

    @ApiOperation("根据id获取医院上架疫苗信息")
    @GetMapping("get/{id}")
    public R getById(@ApiParam(value = "医院上架疫苗ID", required = true) @PathVariable String id) {
        HospitalVaccine vaccine = hospitalVaccineService.getById(id);
        if (vaccine != null) {
            return R.ok().data("item", vaccine);
        } else {
            return R.error().message("数据不存在");
        }
    }


}

